import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.*;

public class MainFrame extends JFrame{
    JButton open=null;
    JButton start=null;
    JComboBox jcb;  //下拉框
    JPanel jp1, jp2,jp3;    //面板
    JLabel jlb1, jlb2,jlb3;
    JScrollPane jsp;    //滚动控件
    JTextField tf1,tf2;
    DownFileUtil downFileUtil;
    JProgressBar progressBar;

    public static void main(String[] args) {
        new MainFrame();
    }
    public MainFrame(){
        //输入要下载的文件地址
//        jp3 = new JPanel();
//        jp3.setBounds(10, 10, 250, 40);
        jlb3 = new JLabel("视频地址：");
        jlb3.setBounds(10, 10, 150, 40);
        tf2 = new JTextField();
        tf2.setBounds(85, 15, 450, 27);
//        jp3.add(jlb3);
//        jp3.add(tf2);
        this.add(jlb3);
        this.add(tf2);

        //下载速度选择
        jp1 = new JPanel();
        jp1.setBounds(10, 60, 250, 40);
        jlb1 = new JLabel("下载速度（并发）：",JLabel.LEFT);
        String str1[] = {"1", "5", "10", "20"};
        jcb = new JComboBox(str1);
        jp1.add(jlb1);
        jp1.add(jcb);
        this.add(jp1);

        //存储目录选择
        jp2 = new JPanel();
        jlb2 = new JLabel("存储路径：",JLabel.LEFT);
        open=new JButton("选择");
        jp2.add(jlb2);
        jp2.add(open);
        jp2.setBounds(10, 110, 150, 40);
        this.add(jp2);

        //存储路径回显
        tf1 = new JTextField();
        tf1.setBounds(160, 113, 220, 27);
        tf1.setEnabled(false);
        this.add(tf1);

        //开始下载按钮
        start=new JButton("开始下载");
        start.setBounds(10, 160, 220, 40);
        this.add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String rootPath = tf1.getText();
                String threadCount = jcb.getSelectedItem().toString();
                String videoUrl = tf2.getText();
                downFileUtil = new DownFileUtil(rootPath,videoUrl,Integer.parseInt(threadCount));
                downFileUtil.start();

                new Thread(){
                    public void run(){
                        //获取当前进度
                        int during = downFileUtil.during;
                        while(during<=100&&!downFileUtil.composeFlag){
                            try{
                                if(during==100&&!downFileUtil.composeFlag){
                                    //为合成完等待
                                    progressBar.setString("视频合成中！");
                                }else{
                                    progressBar.setValue(during);
                                }
                                Thread.sleep(100);
                                during = downFileUtil.during;
                            }catch(InterruptedException e){
                                e.printStackTrace();
                            }
                        }
                        progressBar.setValue(100);
                        progressBar.setString("下载完成！");
                    }
                }.start();
            }
        });

        //下载进度条
        progressBar=new JProgressBar();
        progressBar.setStringPainted(true);
        progressBar.setBounds(10, 260, 220, 40);
        this.add(progressBar);

        this.setLayout(null);
        this.setSize(580, 350);
        this.setLocation(300,200);
        this.setTitle("m3u8视频下载工具-qq交流群208779755");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        open.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc=new JFileChooser();
                jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                String path = file.getAbsolutePath();
                tf1.setText(path);
            }
        });
    }

}